package com.example.musictutor.services.impl;

import android.content.Context;
import android.util.Log;
import com.example.musictutor.conf.Constants;
import com.example.musictutor.domain.ProgressHistory;
import com.example.musictutor.repository.facade.impl.ProgressHistoryDAOFacade;
import com.example.musictutor.services.ChartService;

import java.util.HashMap;
import java.util.List;

/**
 * Created by ironhulk on 2014/05/07.
 */
public class ChartServiceImpl implements ChartService {

    private List<ProgressHistory> progressHistory;
    private ProgressHistoryDAOFacade phf;
    @Override
    public void setProgressLevelStats(HashMap<String,Integer> progressStatsPerLevel,Context context){

        phf = new ProgressHistoryDAOFacade(context);
        try {
            progressHistory = phf.getEntities();
        }catch (NullPointerException ne){
            Log.d(Constants.LOG_CHART_SERVICE_INIT,"Error during initialization: Null Pointer");
        }
    }

    @Override
    public String renderUserProgressChartForLevel() {
        String htmlToReturn = "";
        try {
            htmlToReturn =
                    "<html>\n" +
                            "  <head>\n" +
                            "    <script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>\n" +
                            "    <script type=\"text/javascript\">\n" +
                            "      google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});\n" +
                            "      google.setOnLoadCallback(drawChart);\n" +
                            "      function drawChart() {\n" +
                            "        var data = google.visualization.arrayToDataTable([\n" +
                            "          ['Songs Difficulty', 'Amount Of Attempts', 'Amount Passed', 'Amount Failed'],\n" +
                            "          ['EASY', "+phf.getEntities().get(0).getAmountOfAttempts()+", "+phf.getEntities().get(0).getAmountPassed()+", "+phf.getEntities().get(0).getAmountFailed()+"],\n" +
                            "          ['MEDIUM', "+phf.getEntities().get(1).getAmountOfAttempts()+", "+phf.getEntities().get(1).getAmountPassed()+", "+phf.getEntities().get(1).getAmountFailed()+"],\n" +
                            "          ['HARD', "+phf.getEntities().get(2).getAmountOfAttempts()+", "+phf.getEntities().get(2).getAmountPassed()+", "+phf.getEntities().get(2).getAmountFailed()+"],\n" +
                            "        ]);\n" +
                            " \n" +
                            "        var options = {\n" +
                            "          title: 'User Progress For Level: "+phf.getEntities().get(0).getLevel()+"',\n" +
                            "          hAxis: {title: 'Songs Difficulty', titleTextStyle: {color: 'red'}}\n" +
                            "        };\n" +
                            " \n" +
                            "        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));\n" +
                            "        chart.draw(data, options);\n" +
                            "      }\n" +
                            "    </script>\n" +
                            "  </head>\n" +
                            "  <body>\n" +
                            "    <div id=\"chart_div\" style=\"width: 1000px; height: 500px;\"></div>\n" +
                            "  </body>\n" +
                            "</html>";
        }catch(Exception e){
            Log.d(Constants.LOG_CHART_SERVICE_INIT,"Error during initialization: Null Pointer");
        }
        return htmlToReturn;
    }
}
